<template>
  <section class="f-transfer">
    <div class="w-card my-content-box">
      <div class="title-box">
        <span class="title-text">核减记录</span>
      </div>

      <div class="my-search-box">
        <div>
          <span>设备编号：</span>
          <el-input v-model="searchForm.deviceNo" clearable placeholder="请输入设备编号" />
        </div>
        <div class="datebox">
          <span>操作时间：</span>
          <el-date-picker v-model="searchForm.date" type="datetimerange" value-format="x" start-placeholder="开始时间"
            end-placeholder="结束时间" @change="selectDate" />
        </div>
        <div class="f-ml-20">
          <el-button @click="searchHandle" type="primary">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </div>
      </div>

      <div class="my-table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'12px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="deviceId" label="设备ID" align="center">
            <template v-slot="scope">
              {{ scope.row.deviceId || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="deviceNo" label="设备编号" align="center">
            <template v-slot="scope">
              {{ scope.row.deviceNo }}
            </template>
          </el-table-column>
          <el-table-column prop="shopName" label="代理商" align="center">
            <template v-slot="scope">
              {{ scope.row.shopName || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="returnIncome" label="退还佣金(元)" align="center">
            <template v-slot="scope">
              {{ scope.row.returnIncome || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="returnJifen" label="退还积分" align="center">
            <template v-slot="scope">
              {{ scope.row.returnJifen || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="createUser" label="操作人" align="center">
            <template v-slot="scope">
              {{ scope.row.createUser || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="操作时间" align="center">
            <template v-slot="scope">
              {{ getDateTime(scope.row.createTime)[6] || '--' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="my-table-footer">
          <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>

    <ConfirmPass ref="confirmPasswordRef" @verification="confirmSuccess" />

  </section>
</template>

<script>
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal, BlobDownLoad } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import ConfirmPass from '@/components/confirmPass.vue'
import api from '../api'

export default {
  components: { ConfirmPass },

  setup() {
    const route = useRoute()
    const router = useRouter()

    const confirmPasswordRef = ref(null)

    const state = reactive({
      searchForm: {
        "createTimeMax": "",
        "createTimeMin": "",
        "deviceNo": "",
        "orderBy": "id desc",
      },

      tableHeight: 0,
      tableData: [],
      page: 1,
      size: 10,
      total: 0,

      confirmData: {}
    })

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        "createTimeMax": "",
        "createTimeMin": "",
        "deviceNo": "",
        "orderBy": "id desc",
      }
      searchHandle()
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
      }
      api.activeRevoteList(obj).then(res => {
        state.tableData = res.data.list
        state.total = res.data.total
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    const selectDate = (e) => {
      state.searchForm.createTimeMin = e[0]
      state.searchForm.createTimeMax = e[1]
    }

    function settingHandle(data) {
      state.confirmData = data
      const title = "核减提示"
      const msg = '您确定要将所选激活记录核减吗？'
      confirmPasswordRef.value.dialogShow(title, msg)
    }

    const confirmSuccess = () => {
      let json = {
        id: state.confirmData.id
      }
      api.activeRevote(json).then(res => {
        successDeal('操作成功！')
        getTableData()
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 280
      getTableData()
    })

    return {
      ...toRefs(state),
      confirmPasswordRef,
      searchHandle,
      resetHandle,
      handleCurrentChange,
      handleSizeChange,
      getDateTime,
      selectDate,
      settingHandle,
      confirmSuccess,
    }
  }
}
</script>

<style lang="scss" scoped>
.f-transfer {
  .my-content-box {
    box-sizing: border-box;
    padding: 20px;

    > .title-box {
      display: flex;
      align-items: center;

      > .title-text {
        font-size: 16px;
        font-weight: bold;
      }
    }

    .my-search-box {
      box-sizing: border-box;
      padding: 20px 20px 20px 0;
      padding-bottom: 0;
      font-size: 14px;
      display: flex;
      align-items: center;
      gap: 10px;

      > div {
        width: 300px;
        display: flex;
        align-items: center;

        > span {
          flex-shrink: 0;
          width: 90px;
          text-align: right;
        }
      }

      .datebox {
        width: 500px;
      }
    }

    .my-table-box {
      margin-top: 20px;

      .table-handle-box {
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 0 15px;
      }

      .my-table-footer {
        display: flex;
        align-items: center;
        margin-top: 10px;
        flex-wrap: wrap;
        gap: 10px;
      }
    }
  }
}
</style>